#include <stdlib.h>

#include "State.h"
#include "../Interface/Menu.h"
#include "../Interface/Pad.h"
#include "../Core/directory.h"

unsigned char * txtbuffer = NULL;

void UpdateAppState()
{
	switch(s_state)
	{
		case STATE_INIT:
			txtbuffer = (unsigned char *)malloc(BUFFER_SIZE * sizeof(unsigned char)); 
			memset(txtbuffer, 0, BUFFER_SIZE);
			txtlen = 0;
			isSave = 0;
			InitMenu();
			initKeyNote();
			InitDirectory();
			SetState(STATE_MENU);
			break;
		case STATE_MENU:
			DrawMenu(190, 100);
			UpdateMenu();
			break;
		case STATE_FILEIO:
			DrawDirectory(isSave?savefilepath:curfilepath);
			UpdateDirectory(isSave?savefilepath:curfilepath,isSave);
			break;
		case STATE_INITNOTPAD:
			InitPad(txtbuffer, txtlen, 0, rgb2col(0,100,0), 1);
			SetState(STATE_NOTEPAD);
			break;
		case STATE_NOTEPAD:
			DrawKeyBoard(80, 150);
			DrawPad(10, 90);
			if(!m_inpad_menu_active)
			{
				UpdateKeyPad(isSave);
			}
			else
			{
				DrawInPadMenu();
				UpdateInPadMenu();
			}
			break;
		case STATE_SAVE_INIT:
			//SaveFile(curfilepath, dirlist[dir_selected].dirname, txtbuffer, GetPadLen());
			isSave = 1;
			memcpy(savefilepath, curfilepath, NAME_LEN_MAX);
			memcpy(savefilename, dirlist[dir_selected].dirname, NAME_LEN_MAX);
			SetState(STATE_FILEIO);
			break;
		case STATE_SAVE_INPUT:
			InitPad(savefilename, NAME_LEN_MAX, 0, rgb2col(0,100,0), 1);
			SetState(STATE_NOTEPAD);
			break;	
		case STATE_SAVE:
			SaveFile(savefilepath, savefilename, txtbuffer, GetPadLen());
			isSave = 0;
			SetState(STATE_MENU);
			break;					
		case STATE_EXIT:
			break;			
	}
}

void SetState(int state)
{
	if(state >= 0)
		s_state = state;
}